package com.zpf.dao.impl;

import com.zpf.dao.CartDao;
import com.zpf.pojo.Cart;
import com.zpf.pojo.Goods;
import com.zpf.utils.DataSourceUtils;
import com.zpf.utils.TxUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class CartDaoImpl implements CartDao {
    @Override
    public Cart findByUidAndGid(int uid, int pid) {
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select id,pid,Num,money from tb_cart where id = ? and pid = ? ";
        try {
            Cart cart =  qf.query(sql,new BeanHandler<>(Cart.class),uid ,pid);
            return cart;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void insert(Cart cart) {
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "INSERT INTO  tb_cart (id,pid,Num,money) VALUES (?,?,?,?) ";
        try {
          qf.update(sql, cart.getId(),cart.getPid(),cart.getNum(),cart.getMoney());

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("新增失败");
        }

    }

    @Override
    public void updateCart(Cart cart) {
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "UPDATE `tb_cart` SET `Num`=?, `money`=? WHERE (`id`=?) AND (`pid`=?)";
        try {
            qf.update(sql,cart.getNum(),cart.getMoney(),cart.getId(),cart.getPid());

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("修改失败");
        }
    }

    @Override
    public List<Cart> findByUid(int uid) {
        QueryRunner qf= new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select id , pid ,Num , money from tb_cart where id = ?";
        try {
          List<Cart> carts = qf.query(sql,new BeanListHandler<>(Cart.class),uid);
            return carts;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询购物车失败");
        }


        return null;
    }

    @Override
    public void deleteCart(int id, int pid) {
        QueryRunner qf= new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "delete from tb_cart where id = ? and pid = ?";
        try {
            qf.update(sql,id,pid);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("删除失败");
        }


    }

    @Override
    public void clearCart(int uid) {
        Connection conn = TxUtils.getConn();
        QueryRunner qf= new QueryRunner();
        String sql = "delete from tb_cart where id = ?";
        try {
            qf.update(conn,sql,uid);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("清空购物车失败");
        }

    }
}
